package com.recipeexchange

import com.recipeexchange.domain.Recipe
import com.recipeexchange.domain.User
import org.apache.shiro.SecurityUtils
import grails.plugins.nimble.core.UserBase

class RecipeController {
    static scaffold = Recipe

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [recipeInstanceList: Recipe.list(params), recipeInstanceTotal: Recipe.count()]
    }

    def create = {
        Recipe recipeInstance = findOrCreateRecipe()
        render (view: 'create', model:[recipeInstance: recipeInstance])
    }

    def edit = {
        forward(action: "create", params: params)
    }

    def show = {
        def recipeInstance = Recipe.findByLocator(params?.locator ?: "")

        if (!recipeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recipe.label', default: 'Recipe'), params.locator])}"
            redirect(action: "list")
        }
        else {
            [recipeInstance: recipeInstance]
        }
    }

    def loginForComments = {
        redirect(action: "show", params: params)
    }

    def save = {
        Recipe recipeInstance = findOrCreateRecipe()

        def user = getLoggedInUser()

        recipeInstance.user = user
        if (recipeInstance.save(flush: true)) {
            redirect(action: "show", id: recipeInstance.id)
        }
        else {
            render(view: "create", model: [recipeInstance: recipeInstance])
        }
    }

    def update = {
        def recipeInstance = Recipe.get(params.id)
        if (recipeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (recipeInstance.version > version) {

                    recipeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'recipe.label', default: 'Recipe')] as Object[], "Another user has updated this Recipe while you were editing")
                    render(view: "create", model: [recipeInstance: recipeInstance])
                    return
                }
            }
            recipeInstance.properties = params
            if (!recipeInstance.hasErrors() && recipeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'recipe.label', default: 'Recipe'), recipeInstance.id])}"
                redirect(action: "show", id: recipeInstance.id)
            }
            else {
                render(view: "create", model: [recipeInstance: recipeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'recipe.label', default: 'Recipe'), params.id])}"
            redirect(action: "list")
        }
    }

    private Recipe findOrCreateRecipe() {
        def recipeInstance = Recipe.findByLocator(params?.locator ?: "")
        if (!recipeInstance) {
            recipeInstance = new Recipe()
        }
        recipeInstance.properties = params
        return recipeInstance
    }

    private def getLoggedInUser(){
        Long id = SecurityUtils.getSubject()?.getPrincipal()

        if (id) {
            return User.get(id)
        }
    }
}
